package com.jxd.controller;

import com.jxd.model.Employee;
import com.jxd.model.TicketInfo;
import com.jxd.service.ITicketInfoService;
import com.jxd.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ticketInfo")
public class TicketInfoController {

    @Autowired
    private ITicketInfoService ticketInfoService;

    //查找某个员工的所有车票信息
    @PostMapping("/ticketInfoList")
    public Map<String, Object> ticketInfoList(@RequestBody Map<String, String> map) {
        Map<String, Object> map1 = ticketInfoService.getAllTicketInfoList(map);
        return map1;
    }

    @PostMapping("/ticketInfoList01")
    public Map<String, Object> ticketInfoList01(@RequestBody Map<String, String> map) {
        Map<String, Object> map1 = ticketInfoService.getAllTicketInfoList01(map);
        return map1;
    }


    @PostMapping("/getMaxIdTicketInfo/{empno}")
    public TicketInfo getMaxIdTicketInfo(@PathVariable int empno) {
        return ticketInfoService.getMaxIdTicketInfo(empno);
    };


    @RequestMapping("/addTicketInfo")
    public String addTicketInfo(@RequestBody TicketInfo ticketInfo) {
        boolean flag = ticketInfoService.save(ticketInfo);
        if (flag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/updateTicketInfo")
    public String updateTicketInfo(@RequestBody TicketInfo ticketInfo) {
        boolean flag = ticketInfoService.updateById(ticketInfo);
        if (flag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/updateTicketInfoByIds")
    public String updateTicketInfoByIds(@RequestBody Map<String, Object> updateTickets) {
         boolean flag = ticketInfoService.updateTicketInfoByIds(updateTickets);
        if (flag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/delTicketInfo")
    public String delTicketInfo(@RequestBody List<Integer> ids) {
        boolean flag = ticketInfoService.removeByIds(ids);
        if (flag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/getTicketInfo/{id}")
    public TicketInfo getTicketInfo(@PathVariable int id) {
        return ticketInfoService.getById(id);
    }

    @RequestMapping("/isRing")
    public Map<String, Object> isRing(@RequestBody int[] ids) {

        HashMap<String, Object> error = new HashMap<>();
        error.put("data", (Object) null);
        error.put("code", "500");
        error.put("msg", "不是一个闭合环路");
        error.put("count", (Object) null);


        HashMap<String, Object> res = new HashMap<>();
        HashMap<String, Object> travelInfo = new HashMap<>();
        //把int[] 转为 List<Integer>
        List<Integer> list = Arrays.stream(ids).boxed().collect(Collectors.toList());

        List<TicketInfo> ticketInfos = ticketInfoService.listByIds(list);
        res.put("ticketInfos", ticketInfos);

        //判断是否成环
        HashMap<String, String> map = new HashMap<>();
        for (TicketInfo ticketInfo : ticketInfos) {
            if (map.containsKey(ticketInfo.getDeparturePoint()))
                return error;
            map.put(ticketInfo.getDeparturePoint(), ticketInfo.getArrivalLocation());
        }
        System.out.println(map);

        String cur = "济南";
        int i = 1;
        while (!Objects.equals(map.get(cur), "济南")) {
            if (!map.containsKey(cur))
                return error;
            cur = map.get(cur);
            System.out.println(cur);
            i++;
        }

        if (map.size() == i) {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String departureTime, arrivalTime;
            LocalDateTime resDepartureTime = null;
            LocalDateTime resArrivalTime = null;
            double reimbursement = 0;

            for (TicketInfo ticketInfo : ticketInfos) {
                reimbursement += ticketInfo.getReimbursement();
                if (Objects.equals(ticketInfo.getDeparturePoint(), "济南")) {
                    departureTime = ticketInfo.getDepartureTime();
                    travelInfo.put("departureTime", ticketInfo.getDepartureTime());
                    resDepartureTime = LocalDateTime.parse(departureTime, fmt);
                }

                if (Objects.equals(ticketInfo.getArrivalLocation(), "济南")) {
                    arrivalTime = ticketInfo.getArrivalTime();
                    travelInfo.put("arrivalTime", ticketInfo.getArrivalTime());
                    resArrivalTime = LocalDateTime.parse(arrivalTime, fmt);
                }
            }

            LocalDate departureDate = resDepartureTime.toLocalDate();
            LocalDate arrivalDate = resArrivalTime.toLocalDate();
            Period period = Period.between(departureDate, arrivalDate);
            float days = period.get(ChronoUnit.DAYS) + 1;
            System.out.println(days);
            if (resDepartureTime.getHour() > 12) {
                days -= 0.5;
            }

            if (resArrivalTime.getHour() < 12)
                days -= 0.5;
            travelInfo.put("day", days);

            reimbursement += days * 200;
            travelInfo.put("reimbursement", reimbursement);
            travelInfo.put("subsidy", days * 200);

            res.put("travelInfo", travelInfo);
            res.put("code", "200");
            String capitalAmount = String.valueOf(reimbursement);
            res.put("capitalAmount", MoneyUtil.toChinese(capitalAmount));
            return res;
        }
        return error;
    }
}
